function getBranchStation(transferStationId) {
    $.getJSON("../stationManage_querySubStationByParentId.json",{"id":parseInt(transferStationId)},function (data) {
        if(data.success){
            $.each(data.users,function (n,value) {
                $(".branchstation-list").append("<option value='"+value.id+"'>"+value.stationName+"</option>");
            });
        }
    });
};
function getTransferStation(city,area) {
    $.getJSON("../stationManage_findStationOfCity.json",{"city":city,"area":area},function (data) {
        if(data.success){
            $.each(data.stations,function (n,value) {
                $(".transferstation-list").append("<option value='"+value.id+"'>"+value.stationName+"</option>");
            });
        }
    });
};
function getAllCity(parentId){
    $.getJSON("../address_query.json",{parentId : parentId},function(data){
        if(data.success && data.address != null){
            $.each(data.address,function(index,item){
                $(".city-list").append("<option  value="+item.name+">"+item.name+"</option>");
            });
        }
    });
};
$(document).ready(function () {
     getAllCity(0);
    $(".city-list").change(function () {
        $(".transferstation-list option:gt(1)").remove();
        $(".branchstation-list option:gt(1)").remove();
        var city = $(".city-list option:selected").html();
        var area ="";
        getTransferStation(city,area);
    });
    $(".transferstation-list").change(function () {
        $(".branchstation-list option:gt(1)").remove();
        transferStation = $(".transferstation-list option:selected").val();
        getBranchStation(transferStation);
    });
    $(".btn-getInfo").click(function () {
        $(".addExpressNo").val("");
        var transferstationId = $(".transferstation-list option:selected").val();
        var branchstationId = $(".branchstation-list option:selected").val();
        if(transferstationId!=""||branchstationId!=""){
            var args={};
            if(branchstationId!="")
            	args.id=branchstationId;
            else if(transferstationId!="")
            	args.id=transferstationId;
            $.getJSON("../stationManage_StationInfo.json",args,function (data) {
                $("textarea").html(data.user.streets);
                $(".sendMsg-number").text(data.sendNum);
                $(".reciveMsg-number").text(data.recvNum);
                $(".leaveExpressNo").val(data.user.expressNo);
                if(data.user.blance!=""){
                    $(".leaveBalance").val(data.user.blance);
                }
                else {
                    $(".leaveBalance").val(0);
                }
            });
        }
    });
    $(".btn-modifyExpressNo").click(function () {
        var sendNum = $(".sendMsg-number").html();
        var express = $(".leaveExpressNo").val();
        var transferstationId = $(".transferstation-list option:selected").val();
        var branchstationId = $(".branchstation-list option:selected").val();
        if(transferstationId!=""||branchstationId!="") {
            var id;
            if (branchstationId != "")
                id = branchstationId;
            else if (transferstationId != "")
                id = transferstationId;
        }
        $.getJSON("../order_ExpressUpdate.json",{"sendNum":sendNum,"express":express,"id":id},function (data) {
            if(data.success==true){
                window.wxc.xcConfirm(data.msg, window.wxc.xcConfirm.typeEnum.success);
            }else {
                window.wxc.xcConfirm(data.msg, window.wxc.xcConfirm.typeEnum.error);
            }
        })
    });
    $(".btn-submitExpressNo").click(function () {
        var addExpressNo = $(".addExpressNo").val();
        var transferstationId = $(".transferstation-list option:selected").val();
        var branchstationId = $(".branchstation-list option:selected").val();
        if(transferstationId!=""||branchstationId!="") {
            var id;
            if (branchstationId != "")
                id = branchstationId;
            else if (transferstationId != "")
                id = transferstationId;
        }
        $.getJSON("../order_submitExpress.json",{"addExpressNo":addExpressNo,"id":id},function (data) {
            if(data.success==true){
                window.wxc.xcConfirm(data.msg, window.wxc.xcConfirm.typeEnum.success);
                $(".addExpressNo").val("");
            }else {
                window.wxc.xcConfirm(data.msg, window.wxc.xcConfirm.typeEnum.error);
            }
        })
    });
    $(".btn-modifyBalance").click(function () {
        var express = $(".leaveBalance").val();
        var transferstationId = $(".transferstation-list option:selected").val();
        var branchstationId = $(".branchstation-list option:selected").val();
        if(transferstationId!=""||branchstationId!="") {
            var id;
            if (branchstationId != "")
                id = branchstationId;
            else if (transferstationId != "")
                id = transferstationId;
        }
        $.getJSON("../user_BlanceUpdate.json",{"express":express,"id":id},function (data) {
            if(data.success==true){
                window.wxc.xcConfirm(data.msg, window.wxc.xcConfirm.typeEnum.success);
            }else {
                window.wxc.xcConfirm(data.msg, window.wxc.xcConfirm.typeEnum.error);
            }
        })
    });
    $(".btn-submitBalance").click(function () {
        var addBalance = $(".addBalance").val();
        var transferstationId = $(".transferstation-list option:selected").val();
        var branchstationId = $(".branchstation-list option:selected").val();
        if(transferstationId!=""||branchstationId!="") {
            var id;
            if (branchstationId != "")
                id = branchstationId;
            else if (transferstationId != "")
                id = transferstationId;
        }
        $.getJSON("../user_submitBlance.json",{"addBlance":addBalance,"id":id},function (data) {
            if(data.success==true){
                window.wxc.xcConfirm(data.msg, window.wxc.xcConfirm.typeEnum.success);
                $(".addBalance").val("");
            }else {
                window.wxc.xcConfirm(data.msg, window.wxc.xcConfirm.typeEnum.error);
            }
        })
    });
});